package com.easylib.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.easylib.R;


/**
 * Created by gu.yj on 2017/9/19.
 * example:
 * copyPopupWindow.getInstance().init(activity)
 * .showPopupWindow(textView)
 * .setPopOnclickListener(listener);
 */
public class CopyPopupWindow extends PopupWindow {

    private View contentView;//pop布局
    private LayoutInflater inflater;//LayoutInfalter实例
    private LinearLayout rootLayout;//pop的root布局 设置点击事件用
    private TextView longPressView;//传递进来的长按的view 必须是TextView的子类
    private Context context;
    private PopOnclickListener popOnclickListener;//pop点击监听器

    private static class SingleTon {
        private static final CopyPopupWindow INSTANCE = new CopyPopupWindow();
    }

    public static CopyPopupWindow getInstance() {
        return SingleTon.INSTANCE;
    }

    private CopyPopupWindow() {
    }

    public CopyPopupWindow init(Activity activity) {
        context = activity;
        if (inflater == null && contentView == null) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            contentView = inflater.inflate(R.layout.popupwindow_copy_txt, null);
            contentView.measure(0, 0);//手动绘制一次获取宽高，否则第一次取到的为0,0
            this.setContentView(contentView);//设置Pop的View
            this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);//设置Pop的宽
            this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//设置Pop的高
            this.setFocusable(true);//设置Pop可点击
            this.setBackgroundDrawable(new BitmapDrawable());
            this.setOutsideTouchable(true);
            this.setAnimationStyle(R.style.PopupAnimation);//设置Pop动画效果
            rootLayout = (LinearLayout) contentView.findViewById(R.id.pop_root);
            rootLayout.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * pop点击事件
     */
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (popOnclickListener != null && longPressView != null) {
                popOnclickListener.onClick(v, longPressView.getText().toString().trim());
                popdismiss();
            }
        }
    };


    /**
     * 显示Pop
     * @param parent
     */
    public CopyPopupWindow showPopupWindow(TextView parent) {
        longPressView = parent;
        if (!this.isShowing()) {
            int xoff = (parent.getWidth() - contentView.getMeasuredWidth()) / 2;//X轴偏移量
            int yoff = -contentView.getMeasuredHeight() - parent.getHeight();//Y轴偏移量
            this.showAsDropDown(parent, xoff, yoff);
        } else {
            popdismiss();
        }
        return this;
    }

    public CopyPopupWindow setPopOnclickListener(PopOnclickListener popOnclickListener) {
        this.popOnclickListener = popOnclickListener;
        return this;
    }

    private void popdismiss() {
        this.dismiss();
    }

    /**
     * 自定义接口
     */
    public interface PopOnclickListener {
        void onClick(View v, String text);
    }

}
